package com.Xht.www.dao;

import com.Xht.www.po.UserBlacklistPO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserBlacklistDAO extends BaseDAO {
    // 插入用户黑名单信息
    public void insertUserBlacklist(UserBlacklistPO userBlacklistPO) {
        if (!isValidForInsert(userBlacklistPO)) {
            System.err.println("插入用户黑名单信息时，输入数据不合法");
            return;
        }
        String sql = "INSERT INTO user_blacklist (user_id, merchant_name, blacklist_reason, blacklist_time) VALUES (?,?,?,?)";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userBlacklistPO.getUserId());
            pstmt.setString(2, userBlacklistPO.getMerchantName());
            pstmt.setString(3, userBlacklistPO.getBlacklistReason());
            pstmt.setTimestamp(4, userBlacklistPO.getBlacklistTime());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 验证插入操作输入的合法性
    private boolean isValidForInsert(UserBlacklistPO userBlacklistPO) {
        if (userBlacklistPO == null) {
            return false;
        }
        if (userBlacklistPO.getUserId() == null || userBlacklistPO.getUserId() <= 0) {
            return false;
        }
        if (userBlacklistPO.getMerchantName() == null || userBlacklistPO.getMerchantName().trim().isEmpty()) {
            return false;
        }
        if (userBlacklistPO.getBlacklistReason() == null || userBlacklistPO.getBlacklistReason().trim().isEmpty()) {
            return false;
        }
        return userBlacklistPO.getBlacklistTime() != null;
    }

    // 根据用户ID查询所有拉黑记录
    public List<UserBlacklistPO> getUserBlacklistsByUserId(int userId) {
        List<UserBlacklistPO> userBlacklistList = new ArrayList<>();
        String sql = "SELECT blacklist_id, user_id, merchant_name, blacklist_reason, blacklist_time " +
                "FROM user_blacklist WHERE user_id = ?";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    UserBlacklistPO userBlacklistPO = new UserBlacklistPO();
                    userBlacklistPO.setBlacklistId(rs.getInt("blacklist_id"));
                    userBlacklistPO.setUserId(rs.getInt("user_id"));
                    userBlacklistPO.setMerchantName(rs.getString("merchant_name"));
                    userBlacklistPO.setBlacklistReason(rs.getString("blacklist_reason"));
                    userBlacklistPO.setBlacklistTime(rs.getTimestamp("blacklist_time"));
                    userBlacklistList.add(userBlacklistPO);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userBlacklistList;
    }

    // 获取用户拉黑的商家列表
    public List<String> getUserBlacklistedMerchants(int userId) {
        List<String> merchantNames = new ArrayList<>();
        String sql = "SELECT merchant_name FROM user_blacklist WHERE user_id = ?";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    merchantNames.add(rs.getString("merchant_name"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return merchantNames;
    }
}



